﻿@model Sheng.WeixinConstruction.Infrastructure.CouponEntity

@{
    ViewBag.Title = "CouponDistribute";
    Layout = "~/Views/Shared/_LayoutBlank.cshtml";
}

<script type="text/javascript" src="/Scripts/laydate/laydate.js"></script>


<script>

    var _couponId = getQueryString("couponId");
    var _member;

    var _validator_memberSearch;
    var _validator_distribute;

    $(document).ready(function () {

        _validator_memberSearch = $("#form").validate({
            onfocusout: false,
            onkeyup: false,
            showErrors: showValidationErrors,
            rules: {
                "txtSearch_CardNumber": "required"
            },
            messages: {
                "txtSearch_CardNumber": "请输入会员的完整会员卡号查询会员；"
            }
        });

        _validator_distribute = $("#formDistribute").validate({
            onfocusout: false,
            onkeyup: false,
            showErrors: showValidationErrors,
            rules: {
                "txtDistributeCount": {
                    required: true,
                    number: true,
                    digits: true,
                    range: [1, 100]
                }
            },
            messages: {
                "txtDistributeCount": {
                    required: "请输入派发张数",
                    number: "请输入有效整数字；",
                    digits: "请输入有效整数字；",
                    range: "派发张数请介于 1 到 100 之间；"
                }
            }
        });

        $("[keyenter]").keypress(function (e) {
            if (e.keyCode == 13) {
                search();
                //如果不加 return false ，表单会自动 提交且会丢失URL后的参数
                return false;
            }
        });


        $("#txtDistributeCount").keypress(function (e) {
            if (e.keyCode == 13) {
                distribute();
                return false;
            }
        });
    });

    function search() {

        if (_validator_memberSearch.form() == false) {
            return;
        }

        var loadLayerIndex = layer.load(0, {
            shade: [0.2, '#fff']
        });

        var cardNumber = $("#txtSearch_CardNumber").val();

        $.ajax({
            url: "/Api/Member/GetMemberByCardNumber?cardNumber=" + cardNumber,
            type: "POST",
            dataType: "json",
            success: function (data, status, jqXHR) {
                // alert(data);

                layer.close(loadLayerIndex);
                if (data.Success) {

                    _member = data.Data;

                    $("#imageHead").attr("src", _member.Headimgurl_132);

                    $("#spanNickName").html(_member.NickName);
                    $("#spanName").html(_member.Name);
                    $("#spanSex").html(_member.SexString);
                    $("#spanPoint").html(_member.Point);
                    $("#spanCashAccount").html(_member.CashAccount / 100.0);
                    $("#spanCity").html(_member.Country + " " + _member.Province + " " + _member.City);
                    $("#spanMobilePhone").html(_member.MobilePhone);
                    $("#spanEmail").html(_member.Email);
                    $("#spanSubscribeTime").html(_member.SubscribeTime);

                    $("#divMemberInfo").show();
                    $("#divMessage").hide();


                } else {

                    _member = null;

                    $("#divMemberInfo").hide();
                    $("#divMessage").show();

                    layerAlert(data.Message);
                }
            },
            error: function (xmlHttpRequest) {
                layer.close(loadLayerIndex);
                // alert("Error: " + xmlHttpRequest.status);
            }
        });
    }

    function distribute() {

        if (_member == null) {
            layerAlert("请先查询会员后再执行派发。");
            return;
        }

        if (_validator_distribute.form() == false) {
            return;
        }

        var count = $("#txtDistributeCount").val();

        var msg = "是否确认向会员 " + _member.NickName + " 派发 [@Model.Name] " + count + " 张？"

        var confirmLayerIndex = layer.confirm(msg, {
            btn: ['确认', '取消'], //按钮
            shade: [0.4, '#393D49'],
            title: false,
            closeBtn: false,
            shift: _layerShift
        }, function () {
            layer.close(confirmLayerIndex);

            var loadLayerIndex = layer.load(0, {
                shade: [0.2, '#fff']
            });

            var args = new Object();
            args.CouponId = _couponId;
            args.MemberId = _member.Id;
            args.LimitedTime = $("#txtLimitedTime").val();
            args.Count = count;

            $.ajax({
                url: "/Api/Coupon/Distribute",
                type: "POST",
                dataType: "json",
                data: JSON.stringify(args),
                success: function (data, status, jqXHR) {
                    layer.close(loadLayerIndex);
                    if (data.Success) {
                        switch (data.Data.Reason) {
                            case 0:
                                layerAlert("派发成功！", loadDataAndCloseLayer);
                                break;
                            case 1:
                                layerAlert("无权限。");
                                break;
                            case 2:
                                layerAlert("库存不足。");
                                break;
                            case 3:
                                layerAlert("卡券已删除。");
                                break;
                            case 4:
                                layerAlert("卡券不存在。");
                                break;
                            case 5:
                                layerAlert("派发张数无效。");
                                break;
                            default:
                                layerAlert("未知错误：" + data.Data.Reason);
                                break;
                        }

                    } else {
                        layerAlert(data.Message);
                    }
                },
                error: function (xmlHttpRequest) {
                    layer.close(loadLayerIndex);
                    alert("Error: " + xmlHttpRequest.status);
                }
            });
        });
    }

    function loadDataAndCloseLayer() {
        var index = parent.layer.getFrameIndex(window.name);
        parent.loadDataAndCloseLayer(index);
    }

    function cancel() {
        var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
        parent.layer.close(index); //再执行关闭
    }
</script>



<div style="margin-left:20px; margin-right:20px; margin-top:20px;">
    <span id="spanTitle" class="font_black_24">派发</span>
</div>

<div style=" background-color:#ccc; margin-left:20px; margin-right:20px; margin-top:10px; height:1px;">

</div>

<div style=" position:absolute; overflow:auto ;margin-top:25px;left:30px; right:30px; bottom:60px; top:50px; ">

    <form id="form">
        <table width="100%" border="0" cellspacing="0" cellpadding="0">
            <tr>
                <td width="110" height="36">会员卡号：</td>
                <td>
                    <table width="100%" border="0" cellspacing="0" cellpadding="0">
                        <tr>
                            <td><input id="txtSearch_CardNumber" name="txtSearch_CardNumber" type="text" class="input_16" style="width:100%; " keyenter /></td>
                            <td align="right" width="90"><input name="btnSearch" type="button" class="btn_white" id="btnSearch" value="查询" onclick="search()" /></td>
                        </tr>
                    </table>

                </td>
            </tr>

        </table>


    </form>

    <div id="divMessage" style="margin-top:20px;">
        请输入会员的完整会员卡号查询会员。
    </div>

    <div id="divMemberInfo" style="margin-top:10px;display:none">

        <div>
            <form id="formDistribute">
                <table width="100%" border="0" cellspacing="0" cellpadding="0">
                    <tr>
                        <td width="110" height="36">派发张数：</td>
                        <td width="130"><input id="txtDistributeCount" name="txtDistributeCount" type="text" class="input_16" style="width:100px; " /></td>
                        <td width="90">有效期：</td>
                        <td width="110"><input id="txtLimitedTime" class="input_16" onclick="laydate({format: 'YYYY-MM-DD'})" /></td>
                        <td><input name="btnDistribute" type="button" class="btn_green" id="btnDistribute" value="派 发" style="margin-left:10px;" onclick="distribute()" /></td>
                    </tr>
                    <tr>
                        <td width="110" height="36"></td>
                        <td colspan="4" class="font_gray_13">有效期留空表示永久有效，有效期当天仍然有效，次日过期；<br/>请注意核对会员信息，派发后不可撤销。</td>
                    </tr>
                </table>
            </form>
        </div>

        <div class="divDotLine" style="margin-top:10px;margin-bottom:10px;"></div>

        <div style="background-color:lightgray;width:132px;height:132px;float:right">
            <img id="imageHead" />
        </div>
        <div style="float:left">
            <table width="100%" border="0" cellspacing="0" cellpadding="0">
                <tr>
                    <td width="110" height="36">微信昵称：</td>
                    <td>
                        <span id="spanNickName"></span>
                    </td>
                </tr>
                <tr>
                    <td height="36">姓名：</td>
                    <td>
                        <span id="spanName"></span>
                    </td>
                </tr>
                <tr>
                    <td height="36">性别：</td>
                    <td>
                        <span id="spanSex"></span>
                    </td>
                </tr>
                <tr>
                    <td height="36">积分：</td>
                    <td>
                        <span id="spanPoint"></span>
                    </td>
                </tr>
                <tr>
                    <td height="36">现金余额：</td>
                    <td>
                        <span id="spanCashAccount"></span>
                    </td>
                </tr>
                <tr>
                    <td height="36">城市：</td>
                    <td>
                        <span id="spanCity"></span>
                    </td>
                </tr>
                <tr>
                    <td height="36">手机号码：</td>
                    <td>
                        <span id="spanMobilePhone"></span>
                    </td>
                </tr>
                <tr>
                    <td height="36">电子邮箱：</td>
                    <td>
                        <span id="spanEmail"></span>
                    </td>
                </tr>
                <tr>
                    <td height="36">关注时间：</td>
                    <td>
                        <span id="spanSubscribeTime"></span>
                    </td>
                </tr>
            </table>
        </div>
        <div style="clear:both"></div>
    </div>

</div>

<div style=" background-color:#ccc; position:absolute; bottom:55px; left:20px;right:20px; height:1px;">

</div>

<div style="position:absolute; bottom:15px; left:20px;right:20px;">


    @*<div style="float:left;">
        <form id="formDistribute">
            派发张数：
            <input id="txtDistributeCount" name="txtDistributeCount" type="text" class="input_16" style="width:100px; " />
            <input name="btnDistribute" type="button" class="btn_green" id="btnDistribute" value="派 发" onclick="distribute()" />
        </form>
    </div>*@

    <div style="float:right">
        <input name="btnCancel" type="button" class="btn_blue" id="btnCancel" value="取 消" onclick="cancel()" />
    </div>
    <div style="clear:both">
    </div>
</div>